package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.Dept;
import com.ruoyi.system.domain.JingfeiJingfeipici;
import com.ruoyi.system.service.IDeptService;
import com.ruoyi.system.service.IJingfeiJingfeipiciService;
import com.ruoyi.system.service.impl.JingfeiDanweiquerenServiceImpl;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JingfeiDanweiqueren;
import com.ruoyi.system.service.IJingfeiDanweiquerenService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * danweiquerenController
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Controller
@RequestMapping("/system/danweiqueren")
public class JingfeiDanweiquerenController extends BaseController
{
    private String prefix = "system/danweiqueren";

    @Autowired
    private IJingfeiDanweiquerenService jingfeiDanweiquerenService;

    @Autowired
    private IJingfeiJingfeipiciService jingfeiJingfeipiciService;

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("system:danweiqueren:view")
    @GetMapping()
    public String danweiqueren()
    {
        return prefix + "/danweiqueren";
    }

    /**
     * 查询danweiqueren列表
     */
    @RequiresPermissions("system:danweiqueren:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiDanweiqueren jingfeiDanweiqueren)
    {
        startPage();
        List<JingfeiDanweiqueren> list = jingfeiDanweiquerenService.selectJingfeiDanweiquerenList(jingfeiDanweiqueren);
        return getDataTable(list);
    }

    /**
     * 导出danweiqueren列表
     */
    @RequiresPermissions("system:danweiqueren:export")
    @Log(title = "danweiqueren", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiDanweiqueren jingfeiDanweiqueren)
    {
        List<JingfeiDanweiqueren> list = jingfeiDanweiquerenService.selectJingfeiDanweiquerenList(jingfeiDanweiqueren);
        ExcelUtil<JingfeiDanweiqueren> util = new ExcelUtil<JingfeiDanweiqueren>(JingfeiDanweiqueren.class);
        return util.exportExcel(list, "danweiqueren数据");
    }

    /**
     * 新增danweiqueren
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存danweiqueren
     */
    @RequiresPermissions("system:danweiqueren:add")
    @Log(title = "danweiqueren", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiDanweiqueren jingfeiDanweiqueren)
    {
        return toAjax(jingfeiDanweiquerenService.insertJingfeiDanweiqueren(jingfeiDanweiqueren));
    }

    /**
     * 修改danweiqueren
     */
    @RequiresPermissions("system:danweiqueren:edit")
    @GetMapping("/edit/{querenid}")
    public String edit(@PathVariable("querenid") String querenid, ModelMap mmap)
    {
        JingfeiDanweiqueren jingfeiDanweiqueren = jingfeiDanweiquerenService.selectJingfeiDanweiquerenByQuerenid(querenid);
        mmap.put("jingfeiDanweiqueren", jingfeiDanweiqueren);
        return prefix + "/edit";
    }

    /**
     * 修改保存danweiqueren
     */
    @RequiresPermissions("system:danweiqueren:edit")
    @Log(title = "danweiqueren", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiDanweiqueren jingfeiDanweiqueren)
    {
        return toAjax(jingfeiDanweiquerenService.updateJingfeiDanweiqueren(jingfeiDanweiqueren));
    }

    /**
     * 删除danweiqueren
     */
    @RequiresPermissions("system:danweiqueren:remove")
    @Log(title = "danweiqueren", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiDanweiquerenService.deleteJingfeiDanweiquerenByQuerenids(ids));
    }

    //查询下级单位确认情况
    @PostMapping("finddanweiquerenbyparentid")
    @ResponseBody
    public JsonResult<?> finddanweiquerenbyparentid(@RequestParam("deptid")String ppDeptId, @RequestParam("jingfeipiciid")String ppJingfeipiciId, @RequestParam("deptname")String ppDeptName, @RequestParam("shifouqueren")String ppShifouqueren){

        List<Map<String,Object>> mmList = jingfeiDanweiquerenService.findQuerenByParentid(ppDeptId,ppJingfeipiciId,ppDeptName,ppShifouqueren);

        return JsonResult.getSuccessResult(mmList);
    }

    @PostMapping("/checkqueren")
    @ResponseBody
    public JsonResult<?> checkQueren(@RequestParam("deptid")String ppDeptId)
    {
        try{
            Dept mmDept = deptService.selectDeptByDeptid(ppDeptId);
            String parentid = mmDept.getParentid();

            JingfeiJingfeipici mmJingfeipici = new JingfeiJingfeipici();
            mmJingfeipici.setIsfafang(0l);
            mmJingfeipici.setDeptid(parentid);
            List<JingfeiJingfeipici> mmJingfeiList = jingfeiJingfeipiciService.selectJingfeiJingfeipiciList(mmJingfeipici);

            for (int i=0;i<mmJingfeiList.size();i++){
                String mmPiciId = mmJingfeiList.get(i).getJingfeipiciid();
                JingfeiDanweiqueren tempJingfeiDanweiqueren = new JingfeiDanweiqueren();
                tempJingfeiDanweiqueren.setJingfeipiciid(mmPiciId);
                tempJingfeiDanweiqueren.setDeptid(ppDeptId);
                List<JingfeiDanweiqueren> mmTempList = jingfeiDanweiquerenService.selectJingfeiDanweiquerenList(tempJingfeiDanweiqueren);
                if (mmTempList.size()==1){
                    return JsonResult.getSuccessResult("1");
                }
            }

            return JsonResult.getSuccessResult("0");

        } catch (Exception e) {
            logger.error("danweiqueren -> checkqueren: "+e.getMessage());
            return JsonResult.getErrorResult("检查变动确认失败");
        }
    }

    @PostMapping("/queren")
    @ResponseBody
    public JsonResult<?> queren(@RequestParam("deptid")String ppDeptId)
    {
        try{

            JingfeiDanweiqueren mmJingfeiDanweiqueren = new JingfeiDanweiqueren();
            mmJingfeiDanweiqueren.setQuerenshijian(new Date());
            mmJingfeiDanweiqueren.setDeptid(ppDeptId);
            mmJingfeiDanweiqueren.setDeptname(deptService.selectDeptByDeptid(ppDeptId).getDeptname());
            mmJingfeiDanweiqueren.setShifouqueren(1l);

            boolean isOk = jingfeiDanweiquerenService.queren(mmJingfeiDanweiqueren);

            return isOk==true?JsonResult.getSuccessResult("保存成功"):JsonResult.getErrorResult("保存失败");

        } catch (Exception e) {
            logger.error("danweiqueren -> queren: "+e.getMessage());
            return JsonResult.getErrorResult("添加变动确认失败");
        }
    }
}
